package com.itbaizhan;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class StatementTest {

    public void insertUser(String username,int userage){
        Connection conn = null;
        Statement statement = null;
        try{
            Properties prop = new Properties();
            InputStream is = StatementTest.class.getClassLoader().getResourceAsStream("db.properties");
            prop.load(is);
            String url = prop.getProperty("url");
            String name = prop.getProperty("username");
            String pwd = prop.getProperty("pwd");
            String driver = prop.getProperty("driver");


            Class.forName(driver);
            conn = DriverManager.getConnection(url,name,pwd);
            System.out.println(conn);
            statement = conn.createStatement();
            System.out.println(statement);
            String sql = "insert into users values(default,'"+username+"',"+userage+")";
            boolean flag = statement.execute(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }


    public static void main(String[] args) {
        StatementTest s = new StatementTest();
        s.insertUser("Ale",22);
    }
}
